import logging
import time
import unittest

from parameterized import parameterized
from requests import Session
from api.login_api import LoginApi
from utils import common_assert, read_data


class TestRegisterApi(unittest.TestCase):
    def setUp(self):
        self.session = Session()
        logging.info(F"创建session对象地址{self.session}")
        self.client = LoginApi(self.session)

    def tearDown(self):
        self.session.close()
        logging.info(F"session对象{self.session}被关闭")
    # 登录接口测试
    @parameterized.expand(read_data("login_data.json", "login_api"))
    def test01_login_api(self, keywords, password, expect):
        try:
            resp = self.client.login_api(keywords, password)
            logging.info(f"预取结果: {expect} / 响应结果: {resp.text}")
            if "asd124" == password:
                for i in range(2):
                    resp = self.client.login_api(keywords, password)
                    logging.info(f"预取结果: {expect} / 响应结果: {resp.text}")
                time.sleep(60)
            common_assert(self, resp, expect)
            logging.info("断言通过")
        except Exception as e:
            logging.error(f"断言失败: {e}")
            raise



    # 登录状态接口测试
    @parameterized.expand(read_data("login_data.json", "is_login_api"))
    def test02_is_login_api(self, status, keywords, passwords, expect):
        try:
            if status == "已登录":
                self.client.login_api(keywords, passwords)
            resp = self.client.is_login_api()
            logging.info(f"预取结果: {expect} / 响应结果: {resp.text}")
            common_assert(self, resp, expect)
            logging.info("断言通过")
        except Exception as e:
            logging.error(f"断言失败: {e}")
            raise
